package algotithm.jianzhioffer.test25;


class ListNode {
    int val;
    ListNode next;

    public ListNode(int x) {
        this.val = x;
    }
}

/**
 * @author zhouyanxiang
 * @Date 2020-12-2020/12/21-11:43
 */
public class Solution {


    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(4);
        node1.next = node2;
        node2.next = node3;
        ListNode nodeA = new ListNode(1);
        ListNode nodeB = new ListNode(3);
        ListNode nodeC = new ListNode(4);
        nodeA.next = nodeB;
        nodeB.next = nodeC;
        Solution solution = new Solution();
        ListNode cur = solution.mergeTwoLists(node1, nodeA);
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }


    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        cur.next = l1 == null ? l2 : l1;
        return dummy.next;
    }
}
